﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    public class WenComboBox : ComboBox
    {


        public WenComboBox() : base()
        {
            base.SetStyle(
               //ControlStyles.UserPaint |
               ControlStyles.DoubleBuffer |
               ControlStyles.OptimizedDoubleBuffer |
               ControlStyles.AllPaintingInWmPaint |
               ControlStyles.ResizeRedraw |
               ControlStyles.SupportsTransparentBackColor, true);
            base.UpdateStyles();
            this.BackColor = Color.FromArgb(37, 37, 38);
            this.ForeColor = Color.White;
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.FlatStyle = FlatStyle.Flat;
        }

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern IntPtr GetWindowDC(IntPtr hWnd);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        #region 私有属性
        private Color oneRowColor = Color.FromArgb(37,37,38);
        private Color towRowColor = Color.FromArgb(37, 37, 38);
        private Color mouseRowColor = Color.FromArgb(62,62,64);
        private Color borderColor = Color.FromArgb(0xA7, 0xA6, 0xAA); 
        private Color hotColor = Color.FromArgb(0x33, 0x5E, 0xA8);
        public bool isMouseOver;
        #endregion

        #region 公有属性

        [Category("Wen"), Description("奇数行颜色"), DefaultValue(typeof(Color), "37,37,38")]
        public Color OneRowColor { get => oneRowColor; set { oneRowColor = value; this.Invalidate(); } }

        [Category("Wen"), Description("偶数行颜色"), DefaultValue(typeof(Color), "37,37,38")]
        public Color TowRowColor { get => towRowColor; set { towRowColor = value; this.Invalidate(); } }
        
        [Category("Wen"), Description("鼠标停留颜色"), DefaultValue(typeof(Color), "62,62,64")]
        public Color MouseRowColor { get => mouseRowColor; set { mouseRowColor = value; this.Invalidate(); } }

        #endregion

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            isMouseOver = false;
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            isMouseOver = true;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Graphics g = e.Graphics.SetGDIHigh();
            Rectangle rec = e.Bounds;
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                //rec.Offset(1, 1);
                rec.Inflate(-1, -1);
                using Brush backColor = new SolidBrush(MouseRowColor);
                g.FillRectangle(backColor, rec);
            }
            else if (e.Index % 2 != 1)
            {
                using Brush backColor = new SolidBrush(OneRowColor);
                g.FillRectangle(backColor, rec);
            }
            else
            {
                using Brush backColor = new SolidBrush(TowRowColor);
                g.FillRectangle(backColor, rec);
            }
            if (e.Index < 0)
                return;
            g.DrawString(this.Items[e.Index]?.ToString(), this.Font, new SolidBrush(e.ForeColor), rec,ControlHelper.StringConters);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0xf || m.Msg == 0x133)
            {
                IntPtr hDC = GetWindowDC(m.HWnd);
                if (hDC.ToInt32() == 0) //如果取设备上下文失败则返回
                {
                    return;
                }

                //建立Graphics对像
                using Graphics g = Graphics.FromHdc(hDC).SetGDIHigh();
                Rectangle rec = new Rectangle(0, 0, this.Width, this.Height);
                Rectangle recstr = new Rectangle(1, 0, this.Width - 2, this.Height - 2);
                Rectangle reck = new Rectangle(this.Width - 16, this.Height > 16 ? (this.Height - 16) / 2 : 1, 16 - 2, this.Height > 16 ? 16 : this.Height - 2);
                using Pen pen = new Pen(this.borderColor, 1);

                if (this.isMouseOver)
                {
                    pen.Color = this.hotColor;
                }
                else
                {
                    pen.Color = this.borderColor;
                }

                g.FillRectangle(new SolidBrush(this.BackColor), rec);
                g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
                g.DrawString(Text?.ToString(), this.Font, new SolidBrush(ForeColor), recstr, ControlHelper.StringConters);

                g.DrawImage(WenSkin.Properties.Resources.三角形下, reck);

                //释放DC
                ReleaseDC(m.HWnd, hDC);
            }
        }
    }
}